package com.zetatech.bdp.core.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author cheng.cao@zetatech.com.cn
 */
public class BeanUtils {

    private static final Map<String, Object> beanCache = new ConcurrentHashMap<>();

    /**
     * 单例独享
     *
     * @param className
     * @param <T>
     * @return
     */
    public static <T> T getBean(String className) {
        Object o = beanCache.get(className);
        if (o == null) {
            synchronized (beanCache) {
                if (o == null) {
                    try {
                        Class c = Class.forName(className);
                        o = c.newInstance();
                        beanCache.put(className, o);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return (T) o;
    }
}
